package top.carrotguo.weibo.presenter;

import com.sina.weibo.sdk.auth.Oauth2AccessToken;

import top.carrotguo.weibo.bean.CommentListBean;
import top.carrotguo.weibo.contract.CommentContract;
import top.carrotguo.weibo.model.CommentModel;
import top.carrotguo.weibo.retrofit.callback.BaseCallback;
import top.carrotguo.weibo.retrofit.callback.ErrorInfo;

public class CommentPresenter extends BasePresenter<CommentContract.View> implements CommentContract.Presenter {

    private CommentContract.Model mModel;

    public CommentPresenter(){
        mModel = new CommentModel();
    }

    @Override
    public void getComments(Oauth2AccessToken token, String id,int count,int page) {
        mModel.getComments(token, id, count, page, new BaseCallback<CommentListBean, ErrorInfo>() {
            @Override
            public void onSuccessed(CommentListBean successInfo) {
                if (getView() != null) {
                    if (page == 1) {
                        //第一次加载
                        getView().showLoadingComment();
                        if (successInfo.getComments().size() > 0) {
                            //有评论
                            getView().showCommentsSuccessed(successInfo);
                        } else {
                            //没有评论
                            getView().showCommentsEmpty();
                        }
                        //隐藏加载中
                        getView().hideLoadingComment();
                    } else {
                        //加载更多成功
                        getView().loadMoreSuccess(successInfo);
                    }
                }
            }

            @Override
            public void onError(ErrorInfo errorInfo) {
                if (getView() != null) {
                    if (page == 1) {
                        //隐藏加载中
                        getView().hideLoadingComment();
                        getView().showCommentsFailed(errorInfo);
                    } else {
                        getView().loadMoreFail(errorInfo);
                    }
                }

            }
        });

    }
}
